#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import sys


# 连接MongoDB
def InitMongoDB():
    MONGODB_HOST = 'localhost'
    MONGODB_PORT = 27017
    MONGODB_DB_NAME = 'taxonomy'
    MONGODB_COLLECTION_NAME = 'taxonomy_20170421'
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


def RecurNode(tax_id, collection):
    result = collection.find_one({"tax_id": tax_id})
    print "taxon:" + tax_id + "\t" + result["dms_name"] + "\t" + result["rank"]
    results = collection.find({"parent_tax_id.id": tax_id})
    for item in results:
        RecurNode(item["tax_id"], collection)


if __name__ == '__main__':
    client, collection = InitMongoDB()
    RecurNode(sys.argv[1], collection)
    CloseMongoDB(client)
